clear all
capture log close
capture set virtual off
set memory 1000m
capture set matsize 100000000000
capture set more off
capture virtual off
memory

set type double
set more off
set maxvar 32767
pause on
version 12.1

cd "/Users/Mandywu/Dropbox (MIT)/PNAS revision/code"

*************** code for figure 1
use campaign_all_10wk_prov_nt, replace
keep if campaigntime >= -10 & campaigntime2 <= 10
bysort campaign_treated campaigntime: egen avglog_concentration=mean(log_concentration)

gen log_concentration_control = avglog_concentration if campaign_treated == 0
gen log_concentration_treated = avglog_concentration if campaign_treated == 1

drop if campaigntime == 15

rename log_concentration_treated treated_cities
rename log_concentration_control control_cities

generate age1 = (campaigntime + 5) 
replace  age1 = 0 if campaigntime > -5
generate age2 = (campaigntime + 5) 
replace  age2 = 0 if campaigntime < -4 | campaigntime > -1
generate age3 = (campaigntime + 5) 
replace  age3 = 0 if campaigntime < 0 | campaigntime2 > 0
generate age4 = (campaigntime + 5) 
replace  age4 = 0 if campaigntime2 < 1

generate int1 = 0
replace  int1 = 1 if campaigntime <= -5
generate int2 = 0
replace  int2 = 1 if campaigntime >= -4 & campaigntime <= -1
generate int3 = 0
replace  int3 = 1 if campaigntime >= 0 & campaigntime2 <= 0
generate int4 = 0
replace  int4 = 1 if campaigntime2 >= 1

regress treated_cities int1 int2 int3 int4 age1 age2 age3 age4, hascons
predict yhat_treated

regress control_cities int1 int2 int3 int4 age1 age2 age3 age4, hascons
predict yhat_control

	 
twoway (scatter treated_cities control_cities campaigntime, msize(3pt 3pt)) ///
         (line yhat_treated yhat_control campaigntime if campaigntime <= -5, sort lpattern(solid shortdash) lwidth(medium medthick) lcolor(navy maroon)) ///
		 (line yhat_treated yhat_control campaigntime if campaigntime >= -4 & campaigntime <= -1, sort lpattern(solid shortdash) lwidth(medium medthick) lcolor(navy maroon)) ///
		 (line yhat_treated yhat_control campaigntime if campaigntime >= 0 & campaigntime2 <= 0, sort lpattern(solid shortdash) lwidth(medium medthick) lcolor(navy maroon)) ///
		 (line yhat_treated yhat_control campaigntime if campaigntime2 >= 1, sort lpattern(solid shortdash) lwidth(medium medthick) lcolor(navy maroon)), ///
		 xline(-5, lpattern(solid) lcolor(navy)) ///
		 graphregion(color(white)) bgcolor(white) xlabel(-10 "-10" -9 " " -8 " " -7 " " -6 " " -5 " " -4 " " ///
		 -3 " " -2 "Annouce" -1 " " 0 "0" 1 " " 2"On-site" 3 " " 4 " " 5 " " 6 "+1" 7 " " 8 " " 9 " " 10 "+5" 11 " " 12 " " 13 " " 14 " " 15 "+10") ///
		 yline(0, lcolor(black)) xline(-4.5 -0.5, lcolor(gs15)) ///
		 xline(-4, lwidth(6.5) lc(gs15)) xline(-3, lwidth(6.5) lc(gs15)) xline(-2, lwidth(6.5) lc(gs15)) xline(-1, lwidth(6.5) lc(gs15)) ///
		 xline(1 5, lcolor(gs12)) xline(1.5, lwidth(6.5) lc(gs12)) xline(2.5, lwidth(6.5) lc(gs12)) xline(3.5, lwidth(6.5) lc(gs12)) ///
		 xline(4.5, lwidth(6.5) lc(gs12)) xline(0, lpattern(solid) lc(edkblue)) ///
		 xtitle("Week relative to central inspection") ///
		 xlabel(,labsize(medium)) ytitle("log(SO2)", size(3)) graphregion(fcolor(white)) legend(order(1 "Treated cities" 2 "Control cities"))
graph export figure1.png, replace
